package com.ahcloud.edas.gatewaylog.biz.infrastructure.sharding.algorithm;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * @program: ahcloud-operatelog
 * @description:
 * @author: YuKai Fan
 * @create: 2024/6/6 12:52
 **/
public class GatewayLogTableShardingAlgorithm implements PreciseShardingAlgorithm<String> {

    private final static String ACTUAL_TABLE_PREFIX = "gateway_log_";

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
        int value = buildSuffix(shardingValue.getValue());
        return ACTUAL_TABLE_PREFIX + value;
    }

    private int buildSuffix(String apiCode) {
        return  (apiCode.hashCode() & Integer.MAX_VALUE) %  16;
    }
}
